package com.example.demo_hilt.demo_module

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import javax.inject.Qualifier
import javax.inject.Singleton

/**
 * @author yaotianxue
 * @description
 * @date 2025/1/13
 */
@Module
@InstallIn(SingletonComponent::class)
class RetrofitManager {
    @Provides
    @Singleton
    @OkhttpClient1
    fun provideOkhttpClient1():OkHttpClient{
        return OkHttpClient.Builder()
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
//            .addInterceptor() //请求头拦截
            .build()
    }
    @Provides
    @Singleton
    @OkhttpClient2
    fun provideOkhttpClient2():OkHttpClient{
        return OkHttpClient.Builder()
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
//            .addInterceptor()//token
            .build()
    }

    @Provides
    @Singleton
    @Retrofit1
    fun provideRetrofit1(@OkhttpClient1 okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl("网址1")
            .client(okHttpClient)
            .build()
    }
    @Provides
    @Singleton
    @Retrofit2
    fun provideRetrofit2(@OkhttpClient2 okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl("网址2")
            .client(okHttpClient)
            .build()
    }
    @Singleton
    @Provides
    fun provideApiService1(@Retrofit1 retrofit: Retrofit):ApiService1{
        return retrofit.create(ApiService1::class.java)
    }

    @Singleton
    @Provides
    fun provideApiService2(@Retrofit2 retrofit: Retrofit):ApiService2{
        return retrofit.create(ApiService2::class.java)
    }




    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class OkhttpClient1

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class OkhttpClient2


    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class Retrofit1

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class Retrofit2
}